<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'tables');

class KetnoiModelTrackermachine extends JModelLegacy
{
	public function getTable($type = 'Trackermachine', $prefix = 'KetnoiTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	function loadGridData($limitstart,$limit,$where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("t.*,p.ma as ma_machine,p.ten as ten_machine");
        $query->select("c.ma as ma_khachhang,c.ten as tenkhachhang");
        $query->select("v.ma as ma_nhacungcap,v.ten as tennhacungcap");
        $query->from("#__tracker_machine t")
       		  ->innerJoin("#__products p on p.id = t.machine_id")	
       		  ->innerJoin("#__customer c on c.id = t.customer_id")	
       		  ->leftJoin("#__vendor v on v.id = t.vendor_id");
        $query->where($where);
        $query .= " limit $limitstart,$limit";  
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result; 
    }
    
	function loadAllGridData($where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__tracker_machine t")
       		  ->innerJoin("#__products p on p.id = t.machine_id")	
       		  ->innerJoin("#__customer c on c.id = t.customer_id")	
       		  ->leftJoin("#__vendor v on v.id = t.vendor_id");
        $query->where($where); 
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result; 
    }
    
	function save()
    {
    	global $app;
    	$result = array("success"=>true,"error"=>"");
    	$table = $this->getTable();
    	$post = JRequest::get("post");
		$table = Utility::save($table, $post);
		$result["id"] = $table->id;
		$this->insertEmployee($table->id);
		$this->insertLinhkien($table->id);
    	return $result;
    }
    
    function insertEmployee($tracker_id)
    {
    	$employees = JRequest::getVar('employee_id', array(), 'post', 'array');
		$db = JFactory::getDbo();
		$query = "insert into #__tracker_machine_employee values";
		$query_employee = array();
		foreach($employees as $employee_id)
		{
			$query_employee[] = "($tracker_id,$employee_id)";
		}
		if(count($query_employee) > 0)
		{
			$query .= implode(",", $query_employee);
			$db->setQuery($query);
			$db->query();
		}
    }
    
	function insertLinhkien($tracker_id)
    {
    	$linhkiens = JRequest::getVar('linhkien_id', array(), 'post', 'array');
		$db = JFactory::getDbo();
		$query = "insert into #__tracker_machine_linhkien values";
		$query_linhkien = array();
		foreach($linhkiens as $linhkien_id)
		{
			$vendor = JRequest::getVar("linhkien_vendor_$linhkien_id");
			$sothang = JRequest::getVar("sothangbaohanh_$linhkien_id");
			$sodungluong = JRequest::getVar("sodungluongbaohanh_$linhkien_id");
			$tuoitho = JRequest::getVar("tuoitho_$linhkien_id");
			$query_linhkien[] = "($tracker_id,$linhkien_id,$vendor,$sothang,$sodungluong,$tuoitho)";
		}
		if(count($query_linhkien) > 0)
		{
			$query .= implode(",", $query_linhkien);
			$db->setQuery($query);
			$db->query();
		}
    }
    
    function loadData($id)
    {
    	$table = $this->getTable();
    	$table->load($id);
    	return $table;
    }
    
	function checkSerial()
    {
    	$serial = JRequest::getVar("serial");
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__tracker_machine");
        $query->where("status = 0 and serial = '$serial'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    /*
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		$query = "update #__vendor set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();

	}*/
	
	function loadAllCustomer()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__customer where is_delete = 0";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllEmployee()
	{
		$db = JFactory::getDbo();
		$query = "select p.* from #__user_profile p where p.is_delete = 0 ";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllMayPhoto()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__products where product_type = 'mayphoto' and is_delete = 0";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllHinhthucmay()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__hinhthuc order by ordering ASC";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllVendor()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__vendor where is_delete = 0";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
}
